<?php

namespace App\Controller\Admin;

use App\Constants\ErrorCode;
use App\Controller\InsideService\GiftInside;
use App\Middleware\AdminMiddleware;
use App\Model\QmGameList;
use App\Service\ExcelService;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: '/admin/Gift'), Middleware(AdminMiddleware::class)]
class GiftController extends BaseController {

    /**
     * 获取礼包列表
     * @return array
     */
    #[GetMapping(path: 'index')]
    public function index(): array {
        $userDataArr = $this->uInfo();
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $game_hash = $this->request->query('game_hash', '');
        $page = $this->request->query('page', 1);

        if (empty($game_hash)) {
            $game_hash = $userDataArr['user_data']['bind_games'];
        }

        return (new GiftInside())->setCode($userDataArr['connection_code'])->index(
            $limit, $game_hash, (int)$page
        );
    }

    /**
     * 编辑礼包
     * @return array
     */
    #[PostMapping(path: 'edit')]
    public function edit(): array {
        $userDataArr = $this->uInfo();

        return (new GiftInside())->setCode($userDataArr['connection_code'])->edit($this->request->post());
    }

    /**
     * 删除礼包【会一并删除其关联的礼包码】
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'del')]
    public function del(): array {
        $id = $this->request->query('id');
        $userDataArr = $this->uInfo();

        return (new GiftInside())->setCode($userDataArr['connection_code'])->del($id);
    }

    /**
     * 创建礼包
     * @return array
     * @throws Exception
     */
    #[PostMapping(path: 'add')]
    public function add(): array {
        $userDataArr = $this->uInfo();

        return (new GiftInside())->setCode($userDataArr['connection_code'])->add($this->request->post());
    }

    /**
     * 获取礼包码列表
     * @return array
     * @throws Exception
     */
    #[GetMapping(path: 'code')]
    public function code(): array {
        $limit = $this->request->query('size', config('apiadmin.admin_list_default'));
        $gift_bag_id = $this->request->query('gift_bag_id', '');
        $openid = $this->request->query('openid', '');
        $game_hash = $this->request->query('game_hash', '');
        $page = $this->request->query('page', 1);
        $userDataArr = $this->uInfo();

        return (new GiftInside())->setCode($userDataArr['connection_code'])->code(
            $limit, $gift_bag_id, $openid, $page, $game_hash
        );
    }

    /**
     * 描述: 导出礼包码
     * 日期: 2023/4/25 11:01
     * @return array|ResponseInterface
     * @throws Exception
     * @author: cinder
     */
    #[GetMapping(path: 'export')]
    public function export(): array|ResponseInterface {
        set_time_limit(0);
        ini_set('memory_limit', '5048M');
        $userDataArr = $this->uInfo();
        $gift_bag_id = $this->request->query('gift_bag_id', '');
        $openid = $this->request->query('openid', '');
        $game_hash = $this->request->query('game_hash', '');

        if (empty($gift_bag_id)) {
            return $this->buildFailed(ErrorCode::INVALID, '礼包ID不能为空');
        }
        //导出
        $data = (new GiftInside())->setCode($userDataArr['connection_code'])->code(
            '', $gift_bag_id, $openid, 0, $game_hash//不分页
        );

        if ($data['data']['count'] <= 0) {
            return $this->buildFailed(ErrorCode::INVALID, '1'); //返回标识 前端返回提示
        }

        if ($data['data']['count'] > 1000) {
            return $this->buildFailed(ErrorCode::INVALID, '11'); //返回标识 前端返回提示
        }

        $fileName = '礼包码-' . date('YmdHis');
        $title = [
            'ID',
            'CD-KEY',
            '使用者',
            '使用角色ID',
            '兑换时间',
        ];
        //表体数据

        $list = $data['data']['list'];
        $data = [];
        foreach ($list as $value) {
            $data[] = [
                $value['id'],
                $value['key'],
                $value['openid'],
                $value['role_id'],
                $value['updated_at'],
            ];
        }
        $exportService = new ExcelService();

        //保存临时文件在从浏览器自动下载到本地
        return $exportService->setHeader($title)->addData($data)->saveToBrowserByTmp($fileName);
    }


    /**
     * 生成礼包码
     * @return array
     * @throws Exception
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    #[GetMapping(path: 'buildCode')]
    public function buildCode(): array {
        $number = $this->request->query('number');
        $gift_bag_id = $this->request->query('gift_bag_id', '');
        $game_hash = $this->request->query('game_hash');
        $userDataArr = $this->uInfo();

        return (new GiftInside())->setCode($userDataArr['connection_code'])->buildCode(
            $number, $gift_bag_id, $game_hash
        );
    }
}